package com.rose.redis.high_availability;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * Linux搭建Redis主从参考：https://www.cnblogs.com/diantong/p/11259368.html
 *
 * Master-Slave模式
 * 配置参考：classpath:redisson-master-slave.yml
 * 参考：https://github.com/redisson/redisson/wiki/2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95#281-%E4%B8%BB%E4%BB%8E%E6%A8%A1%E5%BC%8F%E8%AE%BE%E7%BD%AE
 *
 * @author: zhu.chen
 * @date: 2021/1/6
 * @version: v1.0.0
 */
//@Component
public class TestMasterSlaveCommandLineRuner implements CommandLineRunner {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void run(String... args) throws Exception {
        /**
         * 开启org.redisson的debug模式能看到相关读写的日志
         * e.g：
         * stringRedisTemplate.opsForValue().set("age", "11", 1, TimeUnit.DAYS);
         * ||
         * 2021-01-06 21:16:31.766 DEBUG 11424 --- [isson-netty-2-7] org.redisson.command.RedisExecutor       :
         * connection released for command (SETEX) and params [[97, 103, 101], 86400, [49, 49]] from slot NodeSource [slot=0, addr=null, redisClient=null, redirect=null, entry=null]
         * using connection RedisConnection@1475820514 [redisClient=[addr=redis://10.19.133.224:6379], channel=[id: 0x29b631f2, L:/10.19.128.14:49978 - R:/10.19.133.224:6379],
         * currentCommand=CommandData [promise=RedissonPromise [promise=ImmediateEventExecutor$ImmediatePromise@3f128641(success: true)], command=(SETEX), params=[[97, 103, 101], 86400, [49, 49]],
         * codec=org.redisson.client.codec.StringCodec]]
         *
         * "1 : " + stringRedisTemplate.opsForValue().get("age")
         * ||
         * 2021-01-06 21:16:31.769 DEBUG 11424 --- [           main] org.redisson.command.RedisExecutor       :
         * acquired connection for command (GET) and params [[97, 103, 101]] from slot NodeSource [slot=0, addr=null, redisClient=null, redirect=null, entry=null]
         * using node /10.19.133.226:6379... RedisConnection@188217348 [redisClient=[addr=redis://10.19.133.226:6379], channel=[id: 0x4c902580, L:/10.19.128.14:50002 - R:/10.19.133.226:6379],
         * currentCommand=null]
         *
         * "2 : " + stringRedisTemplate.opsForValue().get("age")
         * ||
         * 2021-01-07 13:54:17.514 DEBUG 3188 --- [sson-netty-2-24] org.redisson.command.RedisExecutor       :
         * connection released for command (GET) and params [[97, 103, 101]] from slot NodeSource [slot=0, addr=null, redisClient=null, redirect=null, entry=null]
         * using connection RedisConnection@1988247061 [redisClient=[addr=redis://10.19.131.13:6379], channel=[id: 0x5918b055, L:/10.19.128.14:55705 - R:/10.19.131.13:6379],
         * currentCommand=CommandData [promise=RedissonPromise [promise=ImmediateEventExecutor$ImmediatePromise@49f97cd8(success: [B@40aa86f5)], command=(GET), params=[[97, 103, 101]],
         * codec=org.redisson.client.codec.ByteArrayCodec]]
         *
         */
        stringRedisTemplate.opsForValue().set("age", "11", 1, TimeUnit.DAYS);
        System.out.println("1 : " + stringRedisTemplate.opsForValue().get("age"));
        System.out.println("2 : " + stringRedisTemplate.opsForValue().get("age"));
    }

}
